/*
 *             Copyright (C) 2024 Steve Jrong
 *
 * 	   GitHub Homepage: https://www.github.com/SteveJrong
 *      Gitee Homepage: https://gitee.com/stevejrong1024
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.stevejrong.waiter.unique.id.component.config.test_pre_prod;

import java.util.List;

import org.redisson.Redisson;
import org.redisson.api.RedissonReactiveClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import lombok.AllArgsConstructor;

/**
 * 配置类 - 用于宿主应用的【测试】、【预发布】和【生产】环境的全局ID生成器配置
 * <p>
 * Redisson集群模式配置
 *
 * @author Steve Jrong
 * @since 1.0
 */
@ConditionalOnResource(resources = { "classpath:application-test.yml",
									 	"classpath:application-pre.yml",
										"classpath:application-prod.yml" })
@Configuration
@AllArgsConstructor
public class HostUniqueIdConfig {
	@Value("${waiter.unique-id.cluster.address}")
	private List<String> addressList;
	
	@Value("${waiter.unique-id.cluster.netty-threads}")
	private int nettyThreads;
	
	@Value("${waiter.unique-id.cluster.master-connection-pool-size}")
	private int masterConnectionPoolSize;
	
	@Value("${waiter.unique-id.cluster.slave-connection-pool-size}")
	private int slaveConnectionPoolSize;
	
	@Value("${waiter.unique-id.cluster.retry-interval}")
	private int retryInterval;
	
	@Value("${waiter.unique-id.cluster.timeout}")
	private int timeout;

	/**
	 * 获取响应式的Redisson客户端实例
	 *
	 * @return 响应式的Redisson客户端实例
	 */
	@Bean
	public RedissonReactiveClient waiterUniqueIdComponentRedissonReactiveClient() {
		Config config = new Config();
		
		config.setCodec(new JsonJacksonCodec());
		config.setNettyThreads(nettyThreads);
		config.useClusterServers()
				.setMasterConnectionPoolSize(masterConnectionPoolSize)
				.setSlaveConnectionPoolSize(slaveConnectionPoolSize)
				.setRetryInterval(retryInterval)
				.setTimeout(timeout)
				.setNodeAddresses(addressList);
		
		return Redisson.create(config).reactive();
	}
}